<?php
class html
{
	/** Load and parse static page.
	 *
	 * @param name string, must be one of these:
	 *             book, project, award, teacher.
	 * @param prefix string, 前缀
	 *
	 * @return array(Html_Content).
	 */
	public static function Load($name, $full=true, $prefix='/pub/static/')
	{
		$files = array();
		$allow = array('book', 'project', 'award', 'teacher');
		if (!in_array($name, $allow))
			return $files;

		if ($name=='teacher')
			$path = $_SERVER['DOCUMENT_ROOT'].$prefix;
		else	
			$path = $_SERVER['DOCUMENT_ROOT'].$prefix.$name;
		
		if (!is_dir($path)) return $files;

		$dh = opendir($path);
		while(($f=readdir($dh))!==false) {
			if (!is_dir($path.'/'.$f)) {
				if ($full)
					$files[] = $path.'/'.$f;
				else
					$files[] = $f;
			} // end of if
		} // end of while
		closedir($dh);

		rsort($files);
		return $files;
	}

	/** Save static page.
	 * 
	 * @param name string, page name.
	 * @param cont string, content.
	 */
	public static function Save($name, $file, $cont)
	{
		$allow = array('book', 'project', 'award');
		if (!in_array($name, $allow))
			return;

		$path = $_SERVER['DOCUMENT_ROOT'].'/pub/static/'.$name.'/'
				.$file.'.html';
		$fp = fopen($path, "w");
		fwrite($fp, $cont);
		fclose($fp);
	}

	/** Del static page.
	 *
	 * @param name string, page name.
	 * @param file string, file name.
	 *
	 * @return bool true=success, false=failed.
	 */
	public static function Del($name, $file)
	{
		$allow = array('book', 'project', 'award');
		if (!in_array($name, $allow))
			return false;

		$path = $_SERVER['DOCUMENT_ROOT'].'/pub/static/'.$name.'/'.$file;

		return unlink($path);
	}
	
	public static function DelTeacher($user, $file)
	{
		$path = $_SERVER['DOCUMENT_ROOT'].'/pub/teacher/'.$user.'/'.$file;
		return unlink($path);
	}


	/** 初始化教师个人空间文件夹。
	 * 
	 * @param user string, 教师登录名。
	 */
	public static function InitDir($user)
	{
		$dir = $_SERVER['DOCUMENT_ROOT'].'/pub/teacher/'.$user;
		if (!is_dir($dir)) {
			mkdir($dir, 0775, true);
		}
	}


};
